iT邦幫忙

2025 iThome 鐵人賽

DAY 10
1

牧場主今日工作

昨天我們成功建立了 RKE2 管理叢集,今天要在這個穩固的基礎上安裝 Rancher Server!這就像在我們建好的地基上蓋起漂亮的管理大樓,從此以後我們就能透過網頁介面輕鬆管理整個 Kubernetes 生態系統。準備好迎接數位牧場的管理中心華麗登場吧!

技術背景與概念

https://ithelp.ithome.com.tw/upload/images/20250824/20141794nNVCvndLTS.png

Rancher Server 架構

Rancher Server 是一個基於 Kubernetes 的容器管理平台,提供:

  • 統一管理介面:透過 Web UI 管理多個 Kubernetes 叢集
  • 多叢集管理:從單一介面管理數百個 K8s 叢集
  • RBAC 整合:完整的權限管理和用戶認證
  • 應用商店:內建 Helm Chart 應用程式商店
  • 多租戶支援:Projects 和 Namespaces 隔離機制

Helm 包管理器

為什麼使用 Helm 安裝 Rancher?

  • 版本控制:輕鬆升級和回滾
  • 配置管理:透過 values.yaml 統一設定
  • 依賴處理:自動處理相關組件
  • 標準化部署:官方推薦的安裝方式

前置工具安裝

安裝 Helm 包管理器

# 下載 Helm 安裝腳本
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3

# 設定執行權限
chmod 700 get_helm.sh

# 執行安裝
./get_helm.sh

# 驗證安裝
helm version --short

# 清理安裝檔案
rm get_helm.sh

安裝 K9s(選用但推薦)

K9s 是一個強大的 Kubernetes CLI 管理工具,方便 debug 和監控:

# 下載並安裝 K9s
wget https://github.com/derailed/k9s/releases/latest/download/k9s_linux_amd64.deb
sudo apt install ./k9s_linux_amd64.deb
rm k9s_linux_amd64.deb

# 啟動 K9s
k9s

# 常用快捷鍵:
# :pod - 查看 Pods
# :svc - 查看 Services  
# :ns - 切換 Namespace
# q - 退出

網路基礎設施修復

解決 iptables 相關問題

RKE2 的網路分配依賴 iptables,需要確保正確安裝:

# 安裝 iptables
sudo apt update
sudo apt install -y iptables

# 清理可能的網路配置問題
sudo rm -rf /var/lib/cni/networks/k8s-pod-network

# 重新啟動 RKE2 讓配置生效
sudo systemctl restart rke2-server

# 等待服務完全啟動
sleep 30

# 驗證網路組件
kubectl get pods -n kube-system | grep -E "canal|coredns"

檢查叢集健康狀態

# 確認節點狀態
kubectl get nodes

# 檢查系統 Pods
kubectl get pods -A

# 檢查網路連通性
kubectl run test-pod --image=busybox --rm -it --restart=Never -- ping -c 3 8.8.8.8

Rancher Server 部署

新增 Rancher Helm Repository

# 新增 Rancher 穩定版本倉庫
helm repo add rancher-stable https://releases.rancher.com/server-charts/stable

# 更新 Helm 倉庫
helm repo update

# 驗證倉庫
helm search repo rancher-stable/rancher

配置 Rancher 安裝參數

# 建立 cattle-system namespace(安裝時也會自動建立)
kubectl create namespace cattle-system

# 檢視可用的 Rancher 配置選項
helm show values rancher-stable/rancher | head -50

安裝 Rancher Server

# 使用 Helm 安裝 Rancher
helm install rancher rancher-stable/rancher \
  --namespace cattle-system \
  --set hostname=ithome-rancher.duckdns.org \
  --set tls=external \
  --set replicas=1 \
  --create-namespace

# 檢查安裝狀態
helm status rancher -n cattle-system

重要參數說明

  • hostname: Rancher 的存取域名
  • tls=external: 使用外部 TLS(HAProxy 會處理 SSL)
  • replicas=1: 單實例部署(實驗環境)
  • create-namespace: 自動建立 namespace

監控部署進度

# 即時監控 Pod 狀態
kubectl get pods -n cattle-system -w

# 檢查 Rancher 部署狀態
kubectl rollout status deployment/rancher -n cattle-system

# 查看詳細事件
kubectl get events -n cattle-system --sort-by='.firstTimestamp'

部署成功的標誌:

NAME                       READY   STATUS    RESTARTS   AGE
rancher-5d4b8c7f9b-xxxxx   1/1     Running   0          2m

配置網域解析

修改本機 hosts 檔案(測試用)

# 編輯 hosts 檔案
sudo nano /etc/hosts

# 新增以下行
192.168.0.116 ithome-rancher.duckdns.org

# 驗證解析
ping -c 3 ithome-rancher.duckdns.org

檢查 Rancher Service

# 檢查 Service 配置
kubectl get svc -n cattle-system

# 檢查 Service 詳細資訊
kubectl describe svc rancher -n cattle-system

首次登入設定

存取 Rancher 管理介面

  1. 開啟瀏覽器 前往:https://ithome-rancher.duckdns.org
  2. 忽略 SSL 警告(目前使用 external TLS,HAProxy 尚未配置)
  3. 等待 Rancher 初始化完成

取得初始管理員密碼

# 取得 bootstrap 密碼
kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='{{.data.bootstrapPassword|base64decode}}{{"\n"}}'

# 範例輸出:
# p4ssw0rd123456

完成初始設定

  1. 輸入初始密碼:使用上面取得的密碼登入
  2. 設定新密碼:建立安全的管理員密碼(建議使用密碼管理工具)
  3. 確認 Server URL:檢查 https://ithome-rancher.duckdns.org
  4. 完成設定:進入 Rancher 主控台

https://ithelp.ithome.com.tw/upload/images/20250824/20141794tTfj09B6QN.png

基本功能驗證

檢查 Rancher 狀態

# 檢查 Rancher 部署狀態
kubectl get all -n cattle-system

# 檢查 Rancher 日誌
kubectl logs -n cattle-system deployment/rancher --tail=50

# 檢查資源使用量
kubectl top pods -n cattle-system

故障排除

常見問題處理

1. Rancher Pod 無法啟動

# 檢查詳細錯誤
kubectl describe pod -n cattle-system -l app=rancher

# 檢查資源限制
kubectl describe nodes

2. 無法存取 Web 介面

# 檢查 Service 狀態
kubectl get svc -n cattle-system rancher

# 測試內部連線
kubectl exec -it -n cattle-system deployment/rancher -- curl localhost

3. SSL 相關錯誤

# 檢查 TLS 設定
helm get values rancher -n cattle-system

# 確認使用 external TLS
curl -k https://ithome-rancher.duckdns.org

今日總結與明日預告

今天我們成功讓 Rancher Server 華麗登場!數位牧場的管理中心已經準備就緒,你現在可以透過美觀的 Web 介面來管理 Kubernetes 叢集了。

重點回顧:

  • 安裝 Helm 包管理器和 K9s 管理工具
  • 解決網路基礎設施的 iptables 問題
  • 成功部署 Rancher Server 到 RKE2 叢集
  • 完成首次登入和管理員設定
  • 驗證 Web 管理介面功能正常

明天我們將建立第一個 Custom Cluster,學習如何將額外的工作節點加入到我們的數位牧場中,開始體驗 Rancher 的多叢集管理魔法!


💡 牧場主小提示:記得妥善保管 Rancher 的管理員帳號密碼!另外,tls=external 設定表示 SSL 終端會在 HAProxy 處理,這樣可以統一管理憑證。如果 Web 介面載入慢,是正常現象,Rancher 需要一些時間來初始化所有組件!


上一篇
Day 9: RKE2 管理叢集部署實戰
下一篇
Day 11: 基礎設施整合與 Rancher 優化設定
系列文
牧場主的 K8s 放牧日記21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言